package cn.itsource.shop.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.PageBean;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.shop.domain.Employee;
import cn.itsource.shop.domain.Shop;
import cn.itsource.shop.mapper.EmpMapper;
import cn.itsource.shop.mapper.ShopMapper;
import cn.itsource.shop.service.IShopService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
  @Autowired
    private EmpMapper mapper;
  @Autowired
  private LoginInfoMapper loginInfoMapper;

    @Override
    @Transactional
    public void ruzhu(Shop shop) {
        //添加对象
        super.add(shop);
        //获取员工对象
        Employee admin = shop.getAdmin();
        admin.setSalt(StrUtils.getComplexRandomString(10));
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword()+admin.getSalt()));
         LoginInfo loginInfo = creainfo(admin);
        loginInfoMapper.add(loginInfo);
        //System.out.println("lao");
        //System.out.println(admin);
        //设置员工店铺的信息
        admin.setShop(shop);
        //保存之后员工id就有值
        mapper.add(admin);
        //更新
        super.update(shop);
    }

    private LoginInfo creainfo(Employee admin) {
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(admin,info );
        info.setType(0);
        return info;
    }


}
